Skip to content

perf(csr): keep the outer csr hop for optional match with its own where - #230

Merged
prrao87 merged 1 commit into
mainfrom
perf/optional-hop-filter
Nov 28, 2026
Merged

prrao87 merged 1 commit into
mainfrom
perf/optional-hop-filter

Conversation

@prrao87

@prrao87 prrao87 commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Part of #223.

Why

OPTIONAL MATCH (x)-[:T]->(b) runs as one CSR expansion: each row walks its neighbours, and a row with no neighbours is kept once, with nulls for b. Adding a WHERE to the optional clause, as in OPTIONAL MATCH (x)-[:T]->(b) WHERE b.flag = 1, switched the planner to a much slower plan. It planned and ran everything before the clause a second time to find the rows to expand, expanded those, and joined the result back to the original rows with a left join.

On main, the shape suite measures the difference. The filtered query took 3.4 times as long as the unfiltered one in memory mode, and 25 times as long in lazy mode, even though it returns fewer rows.

What changed

  • The optional clause's own WHERE moves into the outer hop. Inside an outer hop, a filter has exactly the semantics OPTIONAL MATCH ... WHERE needs: a neighbour that fails it is skipped, and a row left with no neighbour keeps its row of nulls. When every condition reads only the optional node or relationship, the planner now moves it into the hop, using the same PushCsrTargetFilters step the two-hop degree count already uses.
  • A row counts as matched only after the hop's filters. Before, the outer hop marked a row as matched as soon as it had a neighbour, so a row whose neighbours all failed a filter would have been dropped instead of kept with nulls. Rows are now marked after the target and relationship filters run.
  • Anything else keeps the left join. A WHERE that also reads an earlier variable (WHERE b.age > a.age), or a filter written after the optional clause, can't move into the hop and plans as before.
  • The CSR and operator docs describe the new case.

Validation

  • cargo test --workspace, cargo clippy --workspace --all-targets -- -D warnings, make test (1,822 passed), the strict docs build and typos pass locally. New cases in the OPTIONAL MATCH tests cover a relationship filter, a target and relationship filter together, an OR, and a WHERE that reads an earlier variable, with and without the CSR. The plan test now checks that the first three are a single outer hop and that the last keeps the left join. The same cases also match over Lance datasets. CI has not run yet.
  • All three suites, per CONTRIBUTING.md: release wheels from main (5676cda) and this branch (2a3a159), alternating main and branch twice each, memory and lazy. Every answer check and every shape-suite phrasing and plan check passed. I reran graph-benchmark-ldbc on its own because my builds for the next change overlapped its first round; the numbers below are from the clean rerun.

Shape suite headlines (small graph):

Group Headline Ideal memory: main memory: branch lazy: main lazy: branch
phrasing slowest / fastest of 8 phrasings 1 2.82x 2.83x 7.08x 7.18x
selectivity two filters: slowest / fastest end 1 14.32x 14.77x 11.57x 11.73x
selectivity hop order: slowest / fastest 1 1.54x 1.53x 1.41x 1.44x
count_rows slowest count / fastest rows <= 1 7.03x 7.15x 6.47x 6.54x
hubs cycle: degree 100000 / degree 10 1 1.01x 1.21x 1.01x 1.00x
hubs cycle, hub at both ends: degree 100000 / 10 1 23.05x 24.43x 16.25x 16.45x
hubs pattern predicate: degree 100000 / 10 1 69.30x 70.73x 29.41x 29.61x
hubs cycle at degree 100000: CSR / join <= 1 0.35x 0.36x 0.35x 0.36x
hubs predicate at degree 100000: CSR / join <= 1 3.62x 3.61x 2.94x 2.94x
optional filtered / unfiltered OPTIONAL MATCH <= 1 3.38x 1.03x 24.55x 1.56x
optional filtered OPTIONAL MATCH / filtered MATCH about 1 3.12x 0.94x 15.28x 0.98x
wide_columns cross_filter: notes / ids about 1 4.09x 4.12x 16.87x 16.50x
wide_columns top_k: notes / ids about 1 2.89x 2.89x 13.43x 13.45x
wide_columns far_filter: notes / ids about 1 1.39x 1.41x 6.39x 6.42x
wide_columns all: notes / ids > 1 4.31x 4.22x 17.48x 17.35x
early_exit LIMIT 10 / all rows, ids << 1 0.36x 0.36x 0.41x 0.41x
early_exit LIMIT 10 / all rows, notes << 1 0.64x 0.62x 0.86x 0.92x

optional/optional_where moved by −69% (memory) and −94% (lazy). Two memory-mode hub queries moved in one branch run and matched main in the other: cycle_100000 (1.12 and 1.58 ms against 1.12 and 1.13) and cycle_join_100000, which runs with use_csr=False (3.19 and 4.37 ms against 3.21 and 3.20).

SF1 suites:

Suite Mode Geomean branch / main main run 2 / run 1
graph-benchmark memory 1.004 1.001
graph-benchmark-ldbc memory 0.989 0.981
graph-benchmark-ldbc lazy 1.000 0.994
graph-benchmark-ldbc-complex memory 0.996 0.989
graph-benchmark-ldbc-complex lazy 0.998 1.001

No SF1 query moved by 10% or more.

Machine: Apple M5 MacBook Pro, 24 GB, macOS 26.6.2. Load average 4 to 9, with the high end right after the release build.

Every SF1 query

graph-benchmark, memory

Query main (ms) branch (ms) Change
query1 7.35 7.52 +2%
query2 12.06 12.15 +1%
query3 2.74 2.71 -1%
query4 1.62 1.61 -1%
query5 1.63 1.62 -1%
query6 1.89 1.88 -0%
query7 3.69 3.64 -1%
query8 12.84 13.06 +2%
query9 12.17 12.45 +2%

Geomean branch/main: 1.004; main run 2 / run 1: 1.001 (largest single-query main gap 2%)

graph-benchmark-ldbc, memory

Query main (ms) branch (ms) Change
query1 0.69 0.65 -6%
query2 0.57 0.57 +0%
query3 0.67 0.67 +0%
query4 0.54 0.53 -0%
query5 0.82 0.81 -1%
query6 0.46 0.45 -1%
query7 1.09 1.07 -1%
query8 0.67 0.66 -1%
query9 0.87 0.86 -1%
query10 0.76 0.76 -0%
query11 1.57 1.54 -2%
query12 0.79 0.77 -1%
query13 0.84 0.82 -2%
query14 0.56 0.56 -1%
query15 0.59 0.59 -0%
query16 0.70 0.70 -1%
query17 0.94 0.93 -1%
query18 0.73 0.73 -0%
query19 0.97 0.97 -0%
query20 2.10 2.09 -1%
query21 0.80 0.78 -2%
query22 0.74 0.74 -1%
query23 0.78 0.77 -1%
query24 0.73 0.72 -0%
query25 0.83 0.83 -0%
query26 0.78 0.77 -1%
query27 0.90 0.89 -2%
query28 1.42 1.41 -1%
query29 0.72 0.72 -0%
query30 19.19 18.88 -2%

Geomean branch/main: 0.989; main run 2 / run 1: 0.981 (largest single-query main gap 13%)

graph-benchmark-ldbc, lazy

Query main (ms) branch (ms) Change
query1 1.16 1.16 -0%
query2 1.40 1.41 +0%
query3 0.82 0.81 -1%
query4 0.80 0.80 +0%
query5 2.14 2.17 +1%
query6 0.75 0.75 -0%
query7 1.13 1.12 -0%
query8 1.74 1.75 +1%
query9 1.36 1.35 -1%
query10 0.95 0.95 +0%
query11 1.99 1.97 -1%
query12 6.58 6.66 +1%
query13 1.07 1.07 -1%
query14 0.71 0.71 -0%
query15 0.78 0.78 -0%
query16 1.12 1.13 +1%
query17 1.40 1.40 +0%
query18 1.15 1.16 +0%
query19 1.35 1.36 +1%
query20 16.62 16.52 -1%
query21 0.85 0.85 -0%
query22 0.96 0.95 -0%
query23 1.25 1.25 +0%
query24 1.08 1.08 +0%
query25 1.21 1.21 +0%
query26 1.08 1.08 +0%
query27 1.32 1.32 +0%
query28 2.03 2.03 -0%
query29 1.05 1.04 -1%
query30 19.73 19.40 -2%

Geomean branch/main: 1.000; main run 2 / run 1: 0.994 (largest single-query main gap 3%)

graph-benchmark-ldbc-complex, memory

Query main (ms) branch (ms) Change
query1 25.15 24.94 -1%
query2 2.58 2.57 -0%
query3 25.67 25.59 -0%
query4 5.94 5.93 -0%
query5 71.21 71.79 +1%
query6 21.21 20.92 -1%
query7 10.10 10.04 -1%
query8 2.41 2.42 +1%
query9 36.89 36.77 -0%
query10 20.57 20.70 +1%
query11 3.06 3.05 -1%
query12 16.86 16.37 -3%
query13 1.16 1.18 +1%
query14 249.05 245.97 -1%

Geomean branch/main: 0.996; main run 2 / run 1: 0.989 (largest single-query main gap 4%)

graph-benchmark-ldbc-complex, lazy

Query main (ms) branch (ms) Change
query1 35.10 34.96 -0%
query2 25.08 24.80 -1%
query3 165.84 168.25 +1%
query4 12.93 13.04 +1%
query5 94.55 95.64 +1%
query6 22.88 22.29 -3%
query7 13.60 13.87 +2%
query8 3.38 3.41 +1%
query9 228.20 223.10 -2%
query10 29.06 29.07 +0%
query11 5.47 5.47 -0%
query12 19.52 19.20 -2%
query13 1.44 1.44 +0%
query14 257.14 255.11 -1%

Geomean branch/main: 0.998; main run 2 / run 1: 1.001 (largest single-query main gap 3%)

Every shape-suite query

memory: small graph (20,000 accounts, 229,517 T edges, hub degrees [10, 1000, 10000, 100000])

Runs: main x2, branch x2.

Group Headline Ideal main branch
phrasing slowest / fastest of 8 phrasings 1 2.82x 2.83x
selectivity two filters: slowest / fastest end 1 14.32x 14.77x
selectivity hop order: slowest / fastest 1 1.54x 1.53x
count_rows slowest count / fastest rows <= 1 7.03x 7.15x
hubs cycle: degree 100000 / degree 10 1 1.01x 1.21x
hubs cycle, hub at both ends: degree 100000 / 10 1 23.05x 24.43x
hubs pattern predicate: degree 100000 / 10 1 69.30x 70.73x
hubs cycle at degree 100000: CSR / join <= 1 0.35x 0.36x
hubs predicate at degree 100000: CSR / join <= 1 3.62x 3.61x
optional filtered / unfiltered OPTIONAL MATCH <= 1 3.38x 1.03x
optional filtered OPTIONAL MATCH / filtered MATCH about 1 3.12x 0.94x
wide_columns cross_filter: notes / ids about 1 4.09x 4.12x
wide_columns top_k: notes / ids about 1 2.89x 2.89x
wide_columns far_filter: notes / ids about 1 1.39x 1.41x
wide_columns all: notes / ids > 1 4.31x 4.22x
early_exit LIMIT 10 / all rows, ids << 1 0.36x 0.36x
early_exit LIMIT 10 / all rows, notes << 1 0.64x 0.62x
Query main ms branch ms Change main peak MiB branch peak MiB
phrasing/unnamed_start_inline 0.51 0.51 -0% 1.7 1.6
phrasing/unnamed_start_where 0.51 0.52 +1% 1.8 1.6
phrasing/unnamed_end_inline 0.51 0.50 -1% 1.8 1.6
phrasing/unnamed_end_where 0.52 0.51 -1% 1.7 1.6
phrasing/named_start_inline 1.42 1.40 -1% 10.4 10.3
phrasing/named_start_where 1.44 1.42 -1% 10.5 10.9
phrasing/named_end_inline 1.42 1.41 -1% 10.4 10.5
phrasing/named_end_where 1.44 1.41 -2% 10.6 10.4
selectivity/rows_fwd 10.84 11.00 +1% 2.7 2.6
selectivity/rows_rev 0.76 0.74 -2% 2.1 2.0
selectivity/hop_cheap_first 0.97 0.95 -1% 2.2 2.1
selectivity/hop_hub_first 1.48 1.46 -2% 2.3 2.2
count_rows/count_fwd 5.32 5.29 -1% 3.2 3.0
count_rows/count_rev 5.29 5.33 +1% 3.3 3.0
hubs/cycle_10 1.12 1.12 +0% 2.2 2.1
hubs/cycle_join_10 1.50 1.48 -1% 2.5 2.4
hubs/both_hubs_10 1.08 1.06 -1% 2.1 1.9
hubs/predicate_10 0.59 0.59 +1% 1.7 1.8
hubs/predicate_join_10 2.90 2.87 -1% 4.2 4.1
hubs/cycle_1000 1.13 1.12 -1% 2.2 2.1
hubs/cycle_join_1000 1.50 1.50 +0% 2.6 2.4
hubs/both_hubs_1000 1.25 1.25 +0% 2.1 2.0
hubs/predicate_1000 0.91 0.91 -0% 1.8 1.8
hubs/predicate_join_1000 2.97 2.96 -0% 4.8 5.1
hubs/cycle_10000 1.13 1.09 -4% 2.2 2.1
hubs/cycle_join_10000 1.82 1.78 -2% 3.1 3.1
hubs/both_hubs_10000 3.37 3.64 +8% 2.1 2.0
hubs/predicate_10000 5.14 5.22 +1% 1.8 1.7
hubs/predicate_join_10000 4.01 4.12 +3% 8.6 10.2
hubs/cycle_100000 1.13 1.35 +20% 2.2 2.1
hubs/cycle_join_100000 3.20 3.78 +18% 12.4 15.1
hubs/both_hubs_100000 24.82 25.91 +4% 2.1 2.0
hubs/predicate_100000 40.57 41.66 +3% 1.8 1.7
hubs/predicate_join_100000 11.20 11.55 +3% 54.5 52.8
optional/match 0.60 0.63 +5% 2.4 2.6
optional/optional 0.67 0.67 +0% 2.5 2.2
optional/optional_where 2.25 0.69 -69% 5.6 2.7
optional/match_where 0.72 0.73 +1% 2.6 2.2
wide_columns/far_filter_ids 2.14 2.18 +2% 3.2 3.0
wide_columns/far_filter_notes 2.97 3.08 +4% 25.0 27.4
wide_columns/cross_filter_ids 1.16 1.17 +1% 3.6 3.0
wide_columns/cross_filter_notes 4.74 4.81 +2% 125.9 125.3
wide_columns/top_k_ids 1.35 1.38 +2% 3.6 3.1
wide_columns/top_k_notes 3.91 3.97 +2% 68.4 66.8
wide_columns/all_ids 1.13 1.16 +3% 4.0 3.7
wide_columns/all_notes 4.89 4.91 +0% 187.4 191.8
early_exit/hop_limit_ids 0.39 0.38 -2% 2.0 1.9
early_exit/hop_all_ids 1.07 1.07 -0% 3.3 3.3
early_exit/hop_limit_notes 0.56 0.54 -2% 3.7 3.8
early_exit/hop_all_notes 0.86 0.87 +1% 20.2 20.1
Index main MiB branch MiB main build ms branch build ms
account 18.8 20.7 19 21
ring 28.8 29.0 26 26

main run-to-run geomean: 1.000; largest single-query gap 5%.

lazy: small graph (20,000 accounts, 229,517 T edges, hub degrees [10, 1000, 10000, 100000])

Runs: main x2, branch x2.

Group Headline Ideal main branch
phrasing slowest / fastest of 8 phrasings 1 7.08x 7.18x
selectivity two filters: slowest / fastest end 1 11.57x 11.73x
selectivity hop order: slowest / fastest 1 1.41x 1.44x
count_rows slowest count / fastest rows <= 1 6.47x 6.54x
hubs cycle: degree 100000 / degree 10 1 1.01x 1.00x
hubs cycle, hub at both ends: degree 100000 / 10 1 16.25x 16.45x
hubs pattern predicate: degree 100000 / 10 1 29.41x 29.61x
hubs cycle at degree 100000: CSR / join <= 1 0.35x 0.36x
hubs predicate at degree 100000: CSR / join <= 1 2.94x 2.94x
optional filtered / unfiltered OPTIONAL MATCH <= 1 24.55x 1.56x
optional filtered OPTIONAL MATCH / filtered MATCH about 1 15.28x 0.98x
wide_columns cross_filter: notes / ids about 1 16.87x 16.50x
wide_columns top_k: notes / ids about 1 13.43x 13.45x
wide_columns far_filter: notes / ids about 1 6.39x 6.42x
wide_columns all: notes / ids > 1 17.48x 17.35x
early_exit LIMIT 10 / all rows, ids << 1 0.41x 0.41x
early_exit LIMIT 10 / all rows, notes << 1 0.86x 0.92x
Query main ms branch ms Change main peak MiB branch peak MiB
phrasing/unnamed_start_inline 0.66 0.64 -2% 2.2 2.2
phrasing/unnamed_start_where 0.65 0.66 +1% 2.2 2.1
phrasing/unnamed_end_inline 0.65 0.66 +1% 2.2 2.1
phrasing/unnamed_end_where 0.66 0.65 -1% 2.2 2.1
phrasing/named_start_inline 4.56 4.54 -0% 92.5 92.0
phrasing/named_start_where 4.61 4.61 +0% 92.8 92.4
phrasing/named_end_inline 4.55 4.56 +0% 92.6 92.4
phrasing/named_end_where 4.62 4.60 -0% 92.4 92.6
selectivity/rows_fwd 12.80 12.92 +1% 5.9 5.7
selectivity/rows_rev 1.11 1.10 -0% 3.4 3.3
selectivity/hop_cheap_first 1.28 1.28 -1% 2.8 2.7
selectivity/hop_hub_first 1.82 1.83 +1% 2.8 2.8
count_rows/count_fwd 7.16 7.20 +1% 7.8 7.7
count_rows/count_rev 7.14 7.14 -0% 7.6 7.4
hubs/cycle_10 1.98 2.02 +2% 3.6 3.2
hubs/cycle_join_10 3.32 3.35 +1% 5.4 5.9
hubs/both_hubs_10 1.58 1.57 -1% 2.5 2.2
hubs/predicate_10 1.41 1.40 -1% 2.5 3.1
hubs/predicate_join_10 5.65 5.64 -0% 16.6 13.1
hubs/cycle_1000 1.99 2.02 +2% 4.4 3.3
hubs/cycle_join_1000 3.33 3.38 +1% 6.0 5.9
hubs/both_hubs_1000 1.80 1.80 +0% 2.5 2.3
hubs/predicate_1000 1.81 1.84 +2% 3.5 3.6
hubs/predicate_join_1000 5.75 5.76 +0% 13.4 13.6
hubs/cycle_10000 1.99 2.03 +2% 3.6 3.6
hubs/cycle_join_10000 3.65 3.67 +1% 6.8 7.1
hubs/both_hubs_10000 3.97 3.97 +0% 3.0 2.3
hubs/predicate_10000 6.06 6.02 -1% 2.8 3.4
hubs/predicate_join_10000 6.46 6.44 -0% 19.6 19.0
hubs/cycle_100000 1.99 2.02 +1% 3.7 3.5
hubs/cycle_join_100000 5.71 5.60 -2% 24.3 24.5
hubs/both_hubs_100000 25.68 25.85 +1% 2.5 2.3
hubs/predicate_100000 41.45 41.46 +0% 3.6 3.3
hubs/predicate_join_100000 14.12 14.10 -0% 63.0 58.9
optional/match 0.76 0.75 -0% 3.0 2.8
optional/optional 0.82 0.82 +0% 3.0 2.9
optional/optional_where 20.03 1.28 -94% 26.8 4.0
optional/match_where 1.31 1.31 -0% 4.1 3.9
wide_columns/far_filter_ids 3.05 3.06 +0% 5.9 5.1
wide_columns/far_filter_notes 19.49 19.64 +1% 107.7 107.9
wide_columns/cross_filter_ids 1.45 1.44 -0% 4.0 4.0
wide_columns/cross_filter_notes 24.45 23.82 -3% 252.0 247.6
wide_columns/top_k_ids 1.68 1.67 -1% 3.9 3.8
wide_columns/top_k_notes 22.55 22.45 -0% 230.6 229.6
wide_columns/all_ids 1.41 1.43 +1% 5.6 6.0
wide_columns/all_notes 24.72 24.79 +0% 286.6 289.1
early_exit/hop_limit_ids 0.49 0.49 -0% 2.1 2.4
early_exit/hop_all_ids 1.18 1.18 -0% 5.8 5.8
early_exit/hop_limit_notes 5.41 6.01 +11% 110.5 107.1
early_exit/hop_all_notes 6.27 6.56 +5% 120.1 119.3
Index main MiB branch MiB main build ms branch build ms
account 18.9 19.2 19 19
ring 28.4 28.8 26 26

main run-to-run geomean: 1.003; largest single-query gap 8%.

🤖 Generated with Claude Code

A one-hop OPTIONAL MATCH runs as one outer CsrExpandExec, but any WHERE
of its own sent it back to planning the input a second time and joining
it back. In the shape suite the filtered form was 3.4x (memory) and
25x (lazy) the time of the unfiltered one.

The optional clause's own WHERE has exactly the semantics of a filter
inside an outer hop: a neighbour that fails it is skipped, and a row
left with none keeps one row of nulls. When every conjunct reads only
the optional node or relationship, it now moves into the hop (the same
PushCsrTargetFilters step the degree count uses). Outer rows are marked
matched only after the hop's filters, so a row whose neighbours all
fail keeps its null row. A WHERE that reads earlier variables keeps the
left join.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Deploying lance-csr with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2a3a159
Status: ✅  Deploy successful!
Preview URL: https://523c068e.lance-csr.pages.dev
Branch Preview URL: https://perf-optional-hop-filter.lance-csr.pages.dev

View logs

@prrao87
prrao87 marked this pull request as ready for review November 28, 2026 14:46
@prrao87
prrao87 merged commit ce9790a into main Nov 28, 2026
10 checks passed
@prrao87
prrao87 deleted the perf/optional-hop-filter branch November 28, 2026 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant